package com.tjbank.cssys.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CreditDerate;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CreditDerateService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(tags = "CkLevelController", description = "信用卡减免")
@RestController
@RequestMapping("/derate")
public class CreditDerateController {

    @Autowired
    private CkLogService ckLogService;
    @Autowired
    private CreditDerateService creditDerateService;

    private static final Logger logger = LoggerFactory.getLogger(CreditDerateController.class);


    @ApiOperation("根据批次号,姓名,身份征号,信用卡号获取财务信息")
    @GetMapping("/searchDetails")
    public CommonResult getCkOrderInfoById(@RequestParam Map<String, String> map, HttpServletRequest request) {
        Map result = creditDerateService.getDetailsBybatchNumber(map);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常" + e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("根据批次号,姓名,身份征号,信用卡号获取财务信息");

        if (result != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(result, "123");
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }
    }


    @ApiOperation("查看减免详情")
    @GetMapping("/selectDetails")
    public CommonResult selectDetails(@RequestParam Long id, HttpServletRequest request) {
        CreditDerate creditDerate = creditDerateService.selectByPrimaryKey(id);

        return CommonResult.success(creditDerate, "123");
    }


    @ApiOperation("获取信用卡减免查询管理")
    @GetMapping("/list")
    public CommonResult list(CreditDerate creditDerate, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, HttpServletRequest request) {
        PageInfo pageInfo = creditDerateService.selectList(creditDerate, pageNum, pageSize, request);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        if (pageInfo != null) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            Date date = new Date();
            ckLog.setTimes(new Date(date.getTime()));
            ckLog.setParams(JSON.toJSONString(creditDerate));
            ckLog.setOperationType("查询信用卡减免页面");
            ckLog.setResult("success:200");
            logger.info("查询结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(pageInfo, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
        /*if (CollectionUtils.isNotEmpty(creditDerates)) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(String.valueOf(creditDerate));
            ckLog.setOperationType("查询信用卡减免页面");
            ckLog.setResult("success:200");
            logger.info("查询结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(creditDerates);
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }*/


    }

    @ApiOperation("录入信用卡数据")
    @PostMapping("/add")
    public CommonResult addInsert(@RequestBody CreditDerate record, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("录入信用卡数据");

        int insert = creditDerateService.insert(record);
        if (insert != 0) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            Date date = new Date();
            ckLog.setTimes(new Date(date.getTime()));
            ckLog.setParams(JSON.toJSONString(record));
            ckLog.setOperationType("录入信用卡数据");
            ckLog.setResult("success:200");
            logger.info("录入信用卡数据结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(insert, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }

    @ApiOperation("删除信用卡减免数据")
    @PostMapping("/delete")
    public CommonResult delete(CreditDerate creditDerate, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("逻辑删除信用卡减免数据");
        int i = creditDerateService.UpdateAccountStatus(creditDerate);
        if (String.valueOf(i) != null) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(String.valueOf(creditDerate));
            ckLog.setOperationType("逻辑删除信用卡减免数据");
            ckLog.setResult("success:200");
            logger.info("逻辑删除信用卡减免数据，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(i, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }

    @PostMapping("/audit")
    public CommonResult UpdateAudit(CreditDerate creditDerate, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("审批信用卡减免开始");
        int i = creditDerateService.UpdateAudit(creditDerate);
        if (i != 0) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(JSON.toJSONString(creditDerate));
            ckLog.setOperationType("审批信用卡减免成功");
            ckLog.setResult("success:200");
            logger.info("审批信用卡减免结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(i, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }
}
